###################################
# Sample a speaker from Gaussian.
import torch
import ChatTTS
from IPython.display import Audio
import scipy

chat = ChatTTS.Chat()
chat.load_models(source="local",local_path="pzc163/chatTTS")

texts = ["元思理是个大帅哥，能力非常强，[uv_break]会写代码做法，好多女孩子喜欢他，[laugh]我都很喜欢他。",]
# texts = ['庞依婷，是个优秀教师，[uv_break]又美又可爱，三中一枝花[laugh]']

import torch
std, mean = torch.load('pzc163/chatTTS/asset/spk_stat.pt').chunk(2)

# 随机生成音色
rand_spk = torch.randn(768) * std + mean

# 持久化音色
torch.save(rand_spk, 'tensor.pt')
loaded_tensor = torch.load('tensor.pt')


params_infer_code = {
  'spk_emb': loaded_tensor, # add sampled speaker 
  'temperature': .3, # using custom temperature
  'top_P': 0.7, # top P decode
  'top_K': 20, # top K decode
}

wavs = chat.infer(texts, use_decoder=True,params_infer_code=params_infer_code)
print(wavs)
print("播放")
print(wavs[0])
# Audio(wavs[0], rate=24_000, autoplay=True) 播放不了，保存后才可以播放
scipy.io.wavfile.write(filename = "output.wav", rate = 24_000, data = wavs[0].T)
